package com.myaihui.controller.admin;


import com.myaihui.domain.Account;
import com.myaihui.domain.Market;
import com.myaihui.domain.SysUser;
import com.myaihui.foundation.constant.SysUserConstant;
import com.myaihui.foundation.model.Pager;
import com.myaihui.foundation.query.MarketQuery;
import com.myaihui.service.MarketService;
import com.myaihui.service.SysUserService;
import com.myaihui.util.LoginUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * *******************************
 * Created by Tim.
 * form com.myaihui
 * on 2018/5/30
 * *******************************
 */
@RequestMapping("/market")
@Controller
public class MarketController {

    @Autowired
    private MarketService marketService;

    @Autowired
    private SysUserService sysUserService;

    @RequestMapping(value = "/")
    public ModelAndView home() {
        return new ModelAndView("market/home");
    }

    @RequestMapping(value = "/page")
    @ResponseBody
    public Pager<Market> page(MarketQuery marketQuery) {
        Pager<Market> marketPage = marketService.getMarketPage(marketQuery);
        return marketPage;
    }

    @RequestMapping(value = "/add")
    public ModelAndView add() {
        return new ModelAndView("market/add");
    }

    @RequestMapping(value = "/{id}")
    public ModelAndView detail(@PathVariable String id) {
        ModelAndView modelAndView = new ModelAndView("market/edit");
        Market market = marketService.get(id);
        SysUser sysUser = sysUserService.findByTypeAndPlatformId(SysUserConstant.MARKET_ADMIN, id);
        modelAndView.addObject("market", market);
        modelAndView.addObject("sysUser", sysUser);
        return modelAndView;
    }

    @RequestMapping(value = "/modify")
    public ModelAndView modify(Market market, Account account, String username, String password) {
        marketService.modify(market, account, username, password, LoginUtil.loginUser().getId());
        return new ModelAndView("redirect:/market/");
    }
}